#include "../tools.h"


class Solution {
public:
    int max_int = 0x7fffffff;
    int min_int = 0x80000000;
    int myAtoi(string s) {
        int i = 0;
        int size = s.size();
        while(i < size && s[i] == ' ') ++i;
        if(s[i] != '+' && s[i] != '-' && s[i] > '9' && s[i] < '0') return 0;
        bool flag = true;
        if(s[i] == '-') {flag = false; ++i;}
        long long res = 0L;
        for(; i < size; ++i){
            if(s[i] > '9' || s[i] < '0') break;
            res *= 10;
            res += (s[i] - '0');
        }
        if(!flag) res *= -1;
        if(flag && res > max_int) return max_int;
        if(!flag && res < min_int) return min_int;
        return res;

    }
};

int main(int argc, char const *argv[])
{
    Solution sol;

    string str = "4193 with words";
    cout << sol.myAtoi(str);

    return 0;
}
